package com.rdk.service.impl;


import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rdk.dao.TUserMapper;
import com.rdk.entity.TUser;
import com.rdk.service.TUserService;

@Service
public class TUserServiceImpl extends BaseServiceImpl<TUser>  implements TUserService {

	@Autowired
	private TUserMapper mapper;

	@Override
	public TUser findUserById(String userId) {
		return mapper.selectByPrimaryKey(userId);
	}
	@Override
	//1111
	public JSONArray findUserFriends(String userId) {
        JSONArray array = new JSONArray();
        JSONArray jsonArray = JSONArray.fromObject(mapper.findUserFriends(userId));
        String lastTag = "";
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject json = jsonArray.getJSONObject(i);
            String tagStr = json.getString("tag");
            if (i == 0 || !lastTag.equals(tagStr)) {
                lastTag = tagStr;

                JSONObject tag = new JSONObject();
                tag.put("tag", tagStr);
                array.add(tag);
            }
            json.remove("tag");
            array.add(json);
        }
        return array;
    }
	
	public TUser findUserByName(String userName) {
        return mapper.selectByName(userName);
    }
	
	@Override
	public void saveUserList(List<TUser> userList) {
		mapper.insertBatch(userList);
	}
	
	@Override
	public int updateByPrimaryKeySelective(TUser userInfo) {
		return mapper.updateByPrimaryKeySelective(userInfo);
	}
	
	@Override
	public int insert(TUser userInfo) {
		return mapper.insert(userInfo);
	}
}